package com.ahp.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ahp.constants.ApplicationConstants;
import com.ahp.log.AhpLogFactory;

public class ScoringServlet extends HttpServlet {

	private static final long serialVersionUID = -2147585559274688703L;
	private static final Logger logger = AhpLogFactory.getLogger(Level.ALL);

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String name = "PriorityPercentage.properties";

		PrintWriter out = resp.getWriter();

		Properties percentProps = new Properties();
		File file = new File(getServletContext().getRealPath(
				"/WEB-INF/classes/com/ahp/config/" + name));
		if (file.exists()) {
			try {
				percentProps.load(new FileInputStream(file));
			} catch (FileNotFoundException e) {
				logger.log(Level.SEVERE, "User Name Passwords file missing", e);
			} catch (IOException e) {
				logger.log(Level.SEVERE,
						"User Name Passwords file could not be read", e);
			}
		}

		out.println("<body bgcolor='98AFC7'  style='color: white;'>");
		for (PriorityType priorityType : ApplicationConstants.indicatorsList) {

			ScoringHelper.projection(percentProps, ScoringHelper
					.createPreference(req, priorityType), out, priorityType);

		}
		percentProps.store(new FileOutputStream(file), "");

	}

}
